#include "optionswidget.h"
#include "filetable.h"
#include "slepui.h"


void OptionsWidget::prepareData(){
    int fm = tableFeatureMatrix->getSelection();
    int lv = tableLabelVector->getSelection();
	int gp;
	slep_vector *x;
	slep_matrix *X;

	if(!checkBoxRandomData->isChecked()){
		if(fm<0 || lv<0){
			this->reject();
			return;
		}
		A = new slep_matrix;
		y = new slep_vector;
		if(method == Method_Least_R || method == Method_nnLeast_R || method == Method_Least_C || method == Method_nnLeast_C){ 
			if(tableFeatureMatrix->getSelectionType(fm)== SLEPItem::DENSE_MATRIX){
				A->sparsity = SlepDense;
				tableFeatureMatrix->getDenseMatrix(fm,&(A->m),&(A->n),&(A->data.mtrx));
				A->ld = A->m;
			}
			if(tableLabelVector->getSelectionType(lv)== SLEPItem::DENSE_VECTOR){
				y->inc = 1;
				y->sparsity = SlepDense;
				tableLabelVector->getDenseVector(lv,&(y->dim),&(y->data.vctr));
			}
		}else if(method == Method_Logistic_R || method == Method_nnLogistic_R || method == Method_Logistic_C || method == Method_nnLogistic_C){
			if(tableFeatureMatrix->getSelectionType(fm)== SLEPItem::DENSE_MATRIX){
				A->sparsity = SlepDense;
				tableFeatureMatrix->getDenseMatrix(fm,&(A->m),&(A->n),&(A->data.mtrx));
				A->ld = A->m;
			}
			if(tableLabelVector->getSelectionType(lv)== SLEPItem::DENSE_VECTOR){
				y->inc = 1;
				y->sparsity = SlepDense;
				tableLabelVector->getDenseVector(lv,&(y->dim),&(y->data.vctr));
			}
		}else if(method == Method_glLeast_R || method == Method_glLogistic_R){ 
			gp = tableGroupIndices->getSelection();
			if(tableFeatureMatrix->getSelectionType(fm)== SLEPItem::DENSE_MATRIX){
				A->sparsity = SlepDense;
				tableFeatureMatrix->getDenseMatrix(fm,&(A->m),&(A->n),&(A->data.mtrx));
				A->ld = A->m;
			}
			if(tableLabelVector->getSelectionType(lv)== SLEPItem::DENSE_VECTOR){
				y->inc = 1;
				y->sparsity = SlepDense;
				tableLabelVector->getDenseVector(lv,&(y->dim),&(y->data.vctr));
			}
			if(tableGroupIndices->getSelectionType(gp)== SLEPItem::GROUP_INDICES){
				tableGroupIndices->getGroupIndices(gp,&(opts->ind_size),&(opts->ind));
				(opts->REGISTER) = (OPTS_REGISTRY)((opts->REGISTER)|((int)SlepOPT_indices));
			}
		}else if(method == Method_mtLeast_R){ 
			gp = tableGroupIndices->getSelection();
			if(tableFeatureMatrix->getSelectionType(fm)== SLEPItem::DENSE_MATRIX){
				A->sparsity = SlepDense;
				tableFeatureMatrix->getDenseMatrix(fm,&(A->m),&(A->n),&(A->data.mtrx));
				A->ld = A->m;
			}
			if(tableLabelVector->getSelectionType(lv)== SLEPItem::DENSE_VECTOR){
				y->inc = 1;
				y->sparsity = SlepDense;
				tableLabelVector->getDenseVector(lv,&(y->dim),&(y->data.vctr));
			}
			if(tableGroupIndices->getSelectionType(gp)== SLEPItem::GROUP_INDICES){
				tableGroupIndices->getGroupIndices(gp,&(opts->ind_size),&(opts->ind));
				(opts->REGISTER) = (OPTS_REGISTRY)((opts->REGISTER)|((int)SlepOPT_indices));
			}
		}
	}else{
		int m = dimension1->value();
		int n = dimension2->value();
		A = slepAllocDenseMatrix(m,n);
		y = slepAllocDenseVector(m);
		slep_randn(A->data.mtrx,m*n,0.,1.);
		if(method == Method_Least_R || method == Method_nnLeast_R || method == Method_Least_C || method == Method_nnLeast_C){
			x = slepAllocDenseVector(n);
			slep_randn(x->data.vctr,n,0.,1.); // its xOrin here..
			slep_randn(y->data.vctr,m,0.,1.);
			slep_daAxpby(SlepNoTranspose,1.,A,x,0.01,y);
			slepFreeVector(x);
		}else if(method == Method_Logistic_R || method == Method_nnLogistic_R || method == Method_Logistic_C || method == Method_nnLogistic_C){
			for(int k=0; k< y->dim; k++)
				y->data.vctr[k] = (Uniform(0.0,1.0)>0.5)?1.0:-1.0;
		}else if(method == Method_glLeast_R){
			x = slepAllocDenseVector(n);
			slep_randn(x->data.vctr,n,0.,1.); // its xOrin here..
			slep_randn(y->data.vctr,m,0.,1.);
			slep_daAxpby(SlepNoTranspose,1.,A,x,0.01,y);
			opts->ind_size = 5;
			opts->ind = new int[opts->ind_size];
			opts->ind[0] = 0;
			opts->ind[1] = 100;
			opts->ind[2] = 400;
			opts->ind[3] = 800;
			opts->ind[4] = 1000;
			opts->ind[opts->ind_size-1] = x->dim;
			(opts->REGISTER) = (OPTS_REGISTRY)((opts->REGISTER)|((int)SlepOPT_indices));
			slepFreeVector(x);
		}else if(method == Method_glLogistic_R){
			for(int k=0; k< y->dim; k++)
				y->data.vctr[k] = (Uniform(0.0,1.0)>0.5)?1.0:-1.0;
			opts->ind_size = 5;
			opts->ind = new int[opts->ind_size];
			opts->ind[0] = 0;
			opts->ind[1] = 100;
			opts->ind[2] = 400;
			opts->ind[3] = 800;
			opts->ind[4] = 1000;
			(opts->REGISTER) = (OPTS_REGISTRY)((opts->REGISTER)|((int)SlepOPT_indices));
		}else if(method == Method_mtLeast_R){
			slep_matrix partA;
			slep_vector party;
			int i;
			X = slepAllocDenseMatrix(n,4);
			slep_randn(X->data.mtrx,n*4,0.,1.); // its xOrin here..
			slep_randn(y->data.vctr,m,0.,1.);
			opts->ind_size = 5;
			opts->ind = new int[opts->ind_size];
			opts->ind[0] = 0;
			opts->ind[1] = 100;
			opts->ind[2] = 400;
			opts->ind[3] = 800;
			opts->ind[4] = 1000;
			(opts->REGISTER) = (OPTS_REGISTRY)((opts->REGISTER)|((int)SlepOPT_indices));

			x = new slep_vector;
			x->sparsity = SlepDense;
			
			x->dim = n;
			x->inc = 1;
			
			partA.n = n;
			partA.ld = A->ld;
			partA.sparsity = SlepDense;
			party.inc = 1;
			party.sparsity = SlepDense;
			
			for(i=0; i<5; ++i){
				partA.data.mtrx = &(A->data.mtrx[opts->ind[i]]);
				partA.m = opts->ind[i+1] - opts->ind[i];
				party.data.vctr = &(y->data.vctr[opts->ind[i]]);
				party.dim = opts->ind[i+1] - opts->ind[i];
				x->data.vctr = &X->data.mtrx[i];
				slep_daAxpby(SlepNoTranspose,1.,&partA,x,0.01,&party);
			}
			slepFreeMatrix(X);
			delete x;
		}
	}

	if(itemC0Vector != 0 && hasGot(opts,SlepOPT_c0))
		opts->c0 = FileOperations::slepItem2SlepVector(itemC0Vector);
	
	if(itemX0!=0 && hasGot(opts,SlepOPT_X0))
		opts->X0 = FileOperations::slepItem2SlepMatrix(itemX0);

	if(itemx0!=0 && hasGot(opts,SlepOPT_x0))
		opts->x0 = FileOperations::slepItem2SlepVector(itemx0);

    this->accept();
}


void OptionsWidget::dontRun(){
	reject();
}
